package com.example.yuejian21.nongfusanquan.yuejian21.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yuejian21.nongfusanquan.yuejian21.activity.PostdetailActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Postbean;
import com.example.yuejian21.nongfusanquan.yuejian21.R;

/**
 * Created by Nongfusanquan on 2017-10-10.
 */

public class PostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContex;
    private List mDataList;
    private LayoutInflater mLayoutInflater;

    public PostAdapter(Context mContext, List mDataList) {
        this.mContex =  mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_post, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Postbean entity = (Postbean) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.name.setText(entity.getU_name());
        viewHolder.content.setText(entity.getP_title());
        final String t_picture = "http://10.0.2.2/yuejian14/Public/Admin/postphoto/"+entity.getP_pic();
        if (!t_picture.equals("")) {
            Picasso.with(mContex).load(t_picture).into(viewHolder.photo);

            final String ct_picture = "http://10.0.2.2/yuejian14/Public/Admin/Userpic/"+entity.getU_pic();
            if (!t_picture.equals("")) {
                Picasso.with(mContex).load(ct_picture).into(viewHolder.icon);
            }
    }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();

                bundle.putString("p_id",String.valueOf(entity.getP_id()));
                bundle.putString("u_name",entity.getU_name());
                bundle.putString("p_info",entity.getP_info());
                bundle.putString("u_pic",entity.getU_pic());
                bundle.putString("p_pic",entity.getP_pic());
                bundle.putString("p_createtime",entity.getP_createtime());
                Intent intent=new Intent(mContex, PostdetailActivity.class);
                intent.putExtras(bundle);
                mContex.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView icon,photo;
        TextView name, content;
        public ViewHolder(View itemView) {
            super(itemView);
            content = (TextView) itemView.findViewById(R.id.post_content);
            icon = (ImageView) itemView.findViewById(R.id.post_uicon);
            photo = (ImageView) itemView.findViewById(R.id.post_photo);
            name = (TextView) itemView.findViewById(R.id.post_uname);


        }
    }
}
